package ru.kisa_wst.parse;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Link {

	private String link;
	
	private String protocol;
	
	private String domain;
	
	private String path;
	
	private String file;
	
	private String request;
	
	public Link (String link) {
		this.link = link;
		
		Pattern protocolPattern = Pattern.compile("[^:/]{1,5}");
		Matcher protocolMatcher = protocolPattern.matcher(link);
		if (protocolMatcher.find()) {
			String tmp = protocolMatcher.group();
			if ("http".equalsIgnoreCase(tmp)||
					"https".equalsIgnoreCase(tmp)) {
				protocol = tmp;
			}
		}
		
		Pattern domainPattern = Pattern.compile("://[^/?]*");
		Matcher domainMatcher = domainPattern.matcher(link);
		if (domainMatcher.find()) {
			String tmp = domainMatcher.group();
			domain = tmp.substring(3, tmp.length());
		}
		
		Pattern pathPattern = Pattern.compile("[^/][^?]*");
		Matcher pathMatcher = pathPattern.matcher(link);
		if (pathMatcher.find()) {
			String buf = pathMatcher.group();
			String tmp = protocol + "://" + domain + "/";
			int fileBegin = buf.lastIndexOf("/") + 1;
			if (tmp.length() <= buf.length()) {
				path = buf.substring(tmp.length(), fileBegin);
				file = buf.substring(fileBegin);
			}
		}
		
		Pattern requestPattern = Pattern.compile("[?].*");
		Matcher requestMatcher = requestPattern.matcher(link);
		if (requestMatcher.find()) {
			String tmp = requestMatcher.group();
			request = tmp.substring(1);
		}
	}
	
	public Link(String page, String foundLink) {
		Link pageLink = new Link(page);
		if (foundLink.length() > 6 && "http://".equalsIgnoreCase(foundLink.substring(0, 7))) {
			link = foundLink;
		} else if (foundLink.length() > 7 && "https://".equalsIgnoreCase(foundLink.substring(0, 8))) {
			link = foundLink;
		} else if (foundLink.indexOf("/") == 0) {
			link = pageLink.getProtocol() + "://" +
				pageLink.getDomain() + foundLink;
		} else if (foundLink.indexOf("../") == 0) {
			int count = 0;
			String tmpLink = foundLink;
			while (tmpLink.indexOf("../") == 0) {
				count++;
				tmpLink = tmpLink.substring(3);
				//System.out.println("tmpLink" + tmpLink);
			}
			String baseLink = pageLink.getProtocol() + "://" +
				pageLink.getDomain() + "/" + pageLink.getPath();
			//System.out.println(baseLink);
			if (baseLink.lastIndexOf("/") == baseLink.length()-1) {
				baseLink = baseLink.substring(0, baseLink.length()-1);
			}
			//System.out.println(baseLink);
			int lastIndexOfSlash = baseLink.lastIndexOf("/");
			for (int i=0; i<count; i++) {
				lastIndexOfSlash = baseLink.lastIndexOf("/");
				baseLink = baseLink.substring(0, lastIndexOfSlash);
			}
			link = baseLink + "/" + tmpLink;
		} else if (foundLink.indexOf("./") == 0) {
			String baseLink = pageLink.getProtocol() + "://" + pageLink.getDomain();
			if (pageLink.getPath() != null && pageLink.getPath().length() > 0) {
				baseLink = baseLink + "/" + pageLink.getPath();
			}
			if (baseLink.lastIndexOf("/") != baseLink.length() - 1) {
				baseLink = baseLink + "/";
			}
			link = baseLink + foundLink.substring(2);
		} else {
			String baseLink = pageLink.getProtocol() + "://" + pageLink.getDomain();
			if (pageLink.getPath() != null && pageLink.getPath().length() > 0) {
				baseLink = baseLink + "/" + pageLink.getPath();
			}
			if (baseLink.lastIndexOf("/") != baseLink.length() - 1) {
				baseLink = baseLink + "/";
			}
			link = baseLink + foundLink;
		}
	}
	
	public String getProtocol() {
		return protocol;
	}
	
	public String getDomain() {
		return domain;
	}
	
	public String getPath() {
		return path;
	}
	
	public String getFile() {
		return file;
	}
	
	public String getRequest() {
		return request;
	}
	
	public String getLink() {
		return link;
	}

	public static void main(String[] arg) {
		Link test1 = new Link("http://google.com/path/file?request", "http://yandex.ru/path/file?request");
		System.out.println("HTTP link: " + test1.getLink());
		
		Link test2 = new Link("https://google.com/path/file?request", "http://yandex.ru/path/file?request");
		System.out.println("HTTPS link: " + test2.getLink());
		
		Link test3 = new Link("https://google.com/path/file?request", "/path/file?request");
		System.out.println("Root link: " + test3.getLink());
		
		Link test4 = new Link("https://google.com/path/file?request", "file1?request1");
		System.out.println("This folder link: " + test4.getLink());
		
		Link test5 = new Link("https://google.com/path/file?request", "path1/file1?request1");
		System.out.println("This folder link: " + test5.getLink());
		
		Link test6 = new Link("https://google.com/path/index.php", "path1/file1?request1");
		System.out.println("This folder link: " + test6.getLink());
		
		Link test7 = new Link("https://google.com/path/", "path1/file1?request1");
		System.out.println("This folder link: " + test7.getLink());
		
		Link test8 = new Link("https://google.com/path", "path1/file1?request1");
		System.out.println("This folder link: " + test8.getLink());
		//-----------------------------
		Link test9 = new Link("https://google.com/path1/path2/index.php", "../path3/file1?request1");
		System.out.println("UP link: " + test9.getLink());
		Link test10 = new Link("https://google.com/path1/path2/index.php", "../path3/file1?request1");
		System.out.println("UP link: " + test10.getLink());
		Link test11 = new Link("https://google.com/path1/path2/index.php", "../../path3/file1?request1");
		System.out.println("UP link: " + test11.getLink());
		Link test12 = new Link("https://google.com/path1/path2/1/1/", "../../path3/file1?request1");
		System.out.println("UP link: " + test12.getLink());
		//-----------------------------
		Link test13 = new Link("https://google.com/path1/path2/1/1/index.php?reqw", "./path3/file1?request1");
		System.out.println("This folder link: " + test13.getLink());
		
		Link test14 = new Link("http://kisa/test/", "?../../test=%22");
		System.out.println("This folder link: " + test14.getLink());
		
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(this.link);
		return builder.toString();
	}

	public static void main1(String[] arg) {
		//Link test = new Link("http://google.com/path/path?request");
		Link test = new Link("http://google.com/path/path/2?sfsf");
		
		System.out.println("Link: " + test.getLink());
		System.out.println("Protocol: " + test.getProtocol());
		System.out.println("Domain: " + test.getDomain());
		System.out.println("Path: " + test.getPath());
		System.out.println("File: " + test.getFile());
		System.out.println("Request: " + test.getRequest());
	}
}
